package orbitPrototype.gui;

import orbitPrototype.objects.DisplayObjectContainer;
import orbitPrototype.objects.Sprite;

/**
 * Description:	Dynamically sizeable 3 state button. Button graphics are defined by GUIStyle. 
 * @author Drapan
 *
 */
public class GUIComplexButton extends GUISimpleButton
{
	private String _textLabel = "";
	private GUIStyle _style = null;
	
	private DisplayObjectContainer _buttonComponents = null;
	private DisplayObjectContainer _buttonComponentsHover = null;
	private DisplayObjectContainer _buttonComponentsDown = null;
		
	public GUIComplexButton(int x, int y, int width, int height, GUIWindow parentWindow, String actionLabel, Object actionGlobal, String textLabel, GUIStyle style)
	{
		super(x, y, width, height, actionLabel, actionGlobal, parentWindow);
		
		_style = style;
		_textLabel = textLabel;
		
		init();
	}
	
	public GUIComplexButton(int x, int y, int width, int height, GUIWindow parentWindow, String actionLabel, Object actionGlobal, String textLabel, String style)
	{
		super(x, y, width, height, actionLabel, actionGlobal, parentWindow);
		
		_style = new GUIStyle(style);
		_textLabel = textLabel;
		
		init();
	}
	
	public GUIComplexButton(int x, int y, int width, int height, GUIWindow parentWindow, String actionLabel, Object actionGlobal, String textLabel)
	{
		super(x, y, width, height, actionLabel, actionGlobal, parentWindow);

		_style = new GUIStyle("default");
		_textLabel = textLabel;
		
		init();
	}

	/**
	 * Description:	Initiates the button by adding components and setting visibility.
	 */
	private void init()
	{
		addComponents(_buttonComponents = new DisplayObjectContainer(this.getWidth(), this.getHeight()), "");
		addComponents(_buttonComponentsHover = new DisplayObjectContainer(this.getWidth(), this.getHeight()), "_hover");
		addComponents(_buttonComponentsDown = new DisplayObjectContainer(this.getWidth(), this.getHeight()), "_down");
		
		_buttonComponentsHover.setVisible(false);
		_buttonComponentsDown.setVisible(false);
	}
	
	/**
	 * Description:	Add components to the button. This function is run once for every state of the button,
	 * 				one for "normal", one for "hover" and one for "down". The stateName parameter contains the
	 * 				suffix for the sprite name (example: "_hover").
	 * @param container
	 * @param stateName
	 */
	private void addComponents(DisplayObjectContainer container, String stateName)
	{
		int edgeDimension = _style.getButtonFrameDimension();
		
		// Upper left corner
		container.addChild(new Sprite(0, 0, 0, "gui", _style.getName() + "_button_corner" + stateName));
		// Upper right corner
		container.addChild(new Sprite(this.getWidth() - edgeDimension, 0, Math.PI/2, "gui", _style.getName() + "_button_corner" + stateName));		
		// Lower right corner
		container.addChild(new Sprite(this.getWidth() - edgeDimension, this.getHeight() - edgeDimension, Math.PI, "gui", _style.getName() + "_button_corner" + stateName));		
		// Lower left corner
		container.addChild(new Sprite(0, this.getHeight() - edgeDimension, (3*Math.PI)/2, "gui", _style.getName() + "_button_corner" + stateName));		
		
		// Upper edge
		container.addChild(new Sprite(edgeDimension, 0, this.getWidth() - (edgeDimension * 2), edgeDimension, 0, "gui", _style.getName() + "_button_edge_h" + stateName));
		// Lower edge
		container.addChild(new Sprite(edgeDimension, this.getHeight() - edgeDimension, this.getWidth() - (edgeDimension * 2), edgeDimension, Math.PI, "gui", _style.getName() + "_button_edge_h" + stateName));		
		// Right edge
		container.addChild(new Sprite(this.getWidth() - edgeDimension, edgeDimension, edgeDimension, this.getHeight() - (edgeDimension * 2), 0, "gui", _style.getName() + "_button_edge_v" + stateName));
		// Left edge
		container.addChild(new Sprite(0, edgeDimension, edgeDimension, this.getHeight() - (edgeDimension * 2), Math.PI, "gui", _style.getName() + "_button_edge_v" + stateName));
		
		// Fill
		container.addChild(new Sprite(edgeDimension, edgeDimension, this.getWidth() - (edgeDimension * 2), this.getHeight() - (edgeDimension * 2), 0, "gui", _style.getName() + "_button_fill" + stateName));
		
		// Button text
		GUIText label = new GUIText(this.getWidth() / 2, 0, _textLabel.toUpperCase(), "center", this.getParentWindow());
		label.setY((this.getHeight() / 2) - (label.getHeight() / 2));
		container.addChild(label);
		
		
		this.addChild(container);
	}
	
	@Override
	public void hoverState()
	{
		this.makeOnlyVisible(_buttonComponentsHover);
	}

	@Override
	public void downState()
	{
		this.makeOnlyVisible(_buttonComponentsDown);
	}
	
	@Override
	public void defaultState()
	{
		this.makeOnlyVisible(_buttonComponents);
	}
}
